#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> machine_work_pair;

class comp {
    public:
    bool operator() (machine_work_pair& a, machine_work_pair& b) {
        if (a.first+a.second < (b.first+b.second)) {
            return true;
        } else if (a.first+a.second == (b.first+b.second)) {
            return a.first > b.first;
        } else {
            return false;
        }
    }
};

int main() {
    int task_num;
    cin >> task_num;

    int machine_num;
    vector<priority_queue<machine_work_pair, vector<machine_work_pair>, comp > > task_queue;
    for (int i = 0; i < task_num; ++i) {
        cin >> machine_num;
        priority_queue<machine_work_pair, vector<machine_work_pair>, comp > machine_queue;
        int B, J;
        for (int j = 0; j < machine_num; ++j) {
            cin >> B;
            cin >> J;
            machine_queue.push(machine_work_pair(B, J));
        }
        task_queue.push_back(machine_queue);
    }

    // cal time
    for (int i = 0; i < task_num; ++i) {
        int time = 0;
        machine_work_pair machine;
        int max_time_per_machine = 0;
        while (!task_queue[i].empty()) {
            machine = task_queue[i].top();
            time += machine.first;
            max_time_per_machine = max(max_time_per_machine, machine.first+machine.second);
            task_queue[i].pop();
        }
        time += machine.second;
        max_time_per_machine = max(max_time_per_machine, time);
        cout << max_time_per_machine << endl;
    }
}